package ups.Schedule;

public class ScheduleItem {
	private ScheduleTime 	time;
	private String			shipName;
	private String			leavingFrom;
	private DaysOfWeek		operationDays;
	private int				internalOrder;
	
	public ScheduleItem( ScheduleTime time, String leavingFrom, DaysOfWeek operationDays ) {
		this.time = time;
		this.leavingFrom = leavingFrom.trim();
		
		if( this.leavingFrom.equals( "Seattle-Pier 52" ) ) {
			this.leavingFrom = "Seattle";
		}
		
		this.operationDays = operationDays;
	}
	
	public ScheduleItem( ScheduleTime time, String shipName, String leavingFrom, DaysOfWeek operationDays ) {
		this.time = time;
		this.shipName = shipName.trim();
		this.leavingFrom = leavingFrom.trim();
		
		if( this.leavingFrom.equals( "Seattle-Pier 52" ) ||
			this.leavingFrom.equals( "Seattle - Pier 50")) {
			this.leavingFrom = "Seattle";
		}
		
		this.operationDays = operationDays;
	}

	public ScheduleTime getTime() {
		return time;
	}
	
	public void setTime( ScheduleTime time ) {
		this.time = time;
	}

	public String getLeavingFrom() {
		return leavingFrom;
	}

	public void setLeavingFrom(String leavingFrom) {
		this.leavingFrom = leavingFrom;
	}

	public DaysOfWeek getOperationDays() {
		return operationDays;
	}

	public void setOperationDays(DaysOfWeek operationDays) {
		this.operationDays = operationDays;
	}

	public String getShipName() {
		return shipName;
	}

	public void setShipName(String shipName) {
		this.shipName = shipName;
	}
	
	public String toString() {
		if( leavingFrom == null ) 
			System.out.println( "leavingFrom = NULL ");
		if( time == null )
			System.out.println( "time = NULL ");
		if(	operationDays == null ) {
			System.out.println("operationDays = NULL");
		}
		if( shipName != null ) { 
			return internalOrder + " " + shipName + " leaving from " + leavingFrom + " at " + time.toString() + " " + operationDays.toString() ;
		} else {
			return "A Ship Leaving From "  + leavingFrom + " at " + time.toString() + " " + operationDays.toString() + " " + internalOrder;
		}
	}

	public int getInternalOrder() {
		return internalOrder;
	}

	public void setInternalOrder(int internalOrder) {
		this.internalOrder = internalOrder;
	}
	
	public boolean equals( Object anObject ) {
		if( anObject.getClass().equals( this.getClass() )) {
			return equals( (ScheduleItem)anObject );
		}
		return false;
	}
	
	public boolean equals( ScheduleItem otherItem ) {
		//System.out.print( this.toString() + " vs " + otherItem.toString() );
		if(!(this.getShipName().equals( otherItem.getShipName()))) 
			return false;
		if(this.getOperationDays() != otherItem.getOperationDays())
			return false;
		int h1 = this.getTime().getHours();
		int m1 = this.getTime().getMinutes();
		int h2 = otherItem.getTime().getHours();
		int m2 = otherItem.getTime().getMinutes();
		
		if( h1 != h2 || m1 != m2 )
			return false;
		
	//	System.out.println(" are equal!");
		return true;
	}
	
	public int hashCode() {
		int hc = this.getTime().getHours() + this.getTime().getMinutes();
		//System.out.println( hc );
		return 1;
	}
	
	public boolean isBefore( ScheduleItem otherItem ) {
		int h1 = this.getTime().getHours();
		int m1 = this.getTime().getMinutes();
		int h2 = otherItem.getTime().getHours();
		int m2 = otherItem.getTime().getMinutes();
		
		// These allow us to handle the "wrap around" in the schedule
		if( h1 < 4 ) {	h1 += 24; }
		if( h2 < 4 ) {	h2 += 24; }
		
	//	System.out.printf("%d:%d vs %d:%d\n", h1, m1, h2, m2);
		
		if( h1 < h2 ) {
			return true;
		} else if( h1 == h2 ) {
			if( m1 < m2 ) {
				return true;
			}
		}
		return false;
	}

}